#include "SimpleShader.h"
osg::Program* SimpleShader::_s_sharedProgram2 = 0;
osg::Program* SimpleShader::_s_sharedProgram = 0;
osg::Program* SimpleShader::getProgram()
{
	if (_s_sharedProgram) return _s_sharedProgram;
	std::string vsource =
		"#version 330\n"
		"uniform mat4 osg_ModelViewProjectionMatrix;"
		"uniform mat3 osg_NormalMatrix;"
		"in vec4 osg_Color;"
		"in vec4 osg_Vertex;"
		"in vec3 osg_Normal;"
		"out vec4 color;"
		"void main(){"
		"vec3 lightDir=vec3(0.57735,0.57735,0.57735);"
		"vec3 n=normalize(osg_NormalMatrix*osg_Normal);"
		"float c=max(dot(lightDir,n),0.0);"
		"color=vec4(osg_Color.rgb*c,osg_Color.a);"
		"gl_Position=osg_ModelViewProjectionMatrix*osg_Vertex; }";
	std::string fsource =
		"#version 330\n"
		"in vec4 color;"
		"out vec4 frag;"
		"void main(){frag=color;}";
	osg::Shader* vs = new osg::Shader(osg::Shader::VERTEX, vsource);
	osg::Shader* fs = new osg::Shader(osg::Shader::FRAGMENT, fsource);
	osg::Program* pgm = new osg::Program;
	pgm->addShader(vs);
	pgm->addShader(fs);
	_s_sharedProgram = pgm;
	return _s_sharedProgram;
}

osg::Program* SimpleShader::getProgram2()
{
	if (_s_sharedProgram2) return _s_sharedProgram2;
	std::string vsource =
		"#version 330\n"
		"uniform mat4 osg_ModelViewProjectionMatrix;"
		"uniform mat3 osg_NormalMatrix;"
		"in vec4 osg_Color;"
		"in vec4 osg_Vertex;"
		"out vec4 color;"
		"void main(){"
		"color=osg_Color;"
		"gl_Position=osg_ModelViewProjectionMatrix*osg_Vertex; }";
	std::string fsource =
		"#version 330\n"
		"in vec4 color;"
		"out vec4 frag;"
		"void main(){frag=color;}";
	osg::Shader* vs = new osg::Shader(osg::Shader::VERTEX, vsource);
	osg::Shader* fs = new osg::Shader(osg::Shader::FRAGMENT, fsource);
	osg::Program* pgm = new osg::Program;
	pgm->addShader(vs);
	pgm->addShader(fs);
	_s_sharedProgram2 = pgm;
	return _s_sharedProgram2;
}

